package StackTest;

import java.util.LinkedList;
import java.util.Stack;

public class Test {

    //链表  栈
    public static void main(String[] args) {
        LinkedList<Integer> stack = new LinkedList<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        System.out.println(stack.pop());   //4
        System.out.println(stack.peek());  //3
    }

    //JZ31 栈的压入、弹出序列
    public boolean IsPopOrder(int [] pushA,int [] popA) {
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for (int i = 0; i < pushA.length; i++) {
            stack.push(pushA[i]);
            //j不能超过popA的长度   数组越界
            //stack 不能是空的    空指针异常
            //stack.peek() == popA[j] 万一不是数字呢？？？
            //stack.peek().equals(popA[j])
            while(j<popA.length && !stack.empty() && stack.peek().equals(popA[j])){
                stack.pop();
                j++;
            }

        }
        return stack.empty();
    }



    //判断有效括号
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            //左括号入栈
            if(ch == '(' || ch == '{'||ch == '['){
                stack.push(ch);
            }else {
                //右括号
                //1. 当前栈是不是空的？
                //右括号多了
                if(stack.empty()){
                    return false;
                }
                char ch2 = stack.peek(); //左括号
                if(ch2 == '(' && ch == ')'|| ch2 == '{' && ch == '}'||ch2 == '[' && ch == ']'){
                    stack.pop();
                }else{
                    return false;
                }
            }

        }
        //左括号多了
        if(!stack.empty()){
            return false;
        }
        return true;
    }


    //逆波兰表达式求值
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack  = new Stack<>();
        for (String x:tokens) {
            if(!isOperation(x)){//不是运算符
                //需要转换成整数
                stack.push(Integer.parseInt(x));
            }else{
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch (x){
                    case "+":
                        stack.push(num1+num2);
                        break;
                    case "-":
                        stack.push(num1-num2);
                        break;
                    case "*":
                        stack.push(num1*num2);
                        break;
                    case "/":
                        stack.push(num1/num2);
                        break;
                }
            }
        }
        return stack.pop();
    }

    //判断是不是运算符
    private boolean isOperation(String x){
        if(x.equals("+")||x.equals("-")||x.equals("*")||x.equals("/")){
            return true;
        }
        return false;
    }



    //自己实现的栈
    public static void main2(String[] args) {
        MyStack stack = new MyStack();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        //pop 弹出+返回
        Integer a = stack.pop();
        System.out.println(a);  //3
        //peek  返回
        Integer b = stack.peek();
        Integer b2 = stack.peek();
        System.out.println(b);  //2
        System.out.println(b2);  //2

        //empty
        System.out.println(stack.isEmpty()); //false
    }

    public static void main1(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        //pop 弹出+返回
        Integer a = stack.pop();
        System.out.println(a);  //3
        //peek  返回
        Integer b = stack.peek();
        Integer b2 = stack.peek();
        System.out.println(b);  //2
        System.out.println(b2);  //2

        //size
        System.out.println(stack.size()); //2

        //empty
        System.out.println(stack.empty()); //false


    }
}
